(ns com.yawmark.euler.clj)

;; A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
;; 
;; a^2 + b^2 = c^2
;; For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
;; 
;; There exists exactly one Pythagorean triplet for which a + b + c = 1000.
;; Find the product abc.

(defn- ordered? [a b c]
       (< a b c))

(defn- py? [a b c]
       (= (* c c) (+ (* a a) (* b b))))

(defn- sum [numbers]
       (reduce + numbers))

(defn- sums-to? [n]
       ; (partial = #(sum %) n)
       (fn [numbers] (= (sum numbers) n))
       )

(defn euler-9 [sum]
      (first (for [a (range sum)
                   b (range a sum)
                   c [(- sum a b)]
                   :when (and (ordered? a b c)
                              (py? a b c)
                              ((sums-to? sum) [a b c]))]
                  [a b c])))

(let [result (time (euler-9 1000))]
  (println (str "The numbers are: " result))
  (println (str "The product is: " (reduce * result))))
